package com.eastfair.transport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.transport.service.HomeSpaceService;
import com.eastfair.venuebooking.api.ServiceProviderSpaceFeign;
import com.eastfair.venuebooking.api.SpaceAcceptanceFeign;
import com.eastfair.venuebooking.vo.SpaceVO;
import com.eastfair.venueservice.dto.ExhibitionServiceProviderDTO;
import com.eastfair.venueservice.dto.ServiceProviderSpaceQuery;
import com.eastfair.venueservice.dto.SpaceAcceptanceQuery;
import com.eastfair.venueservice.enumeration.SpaceAcceptanceFinishStatusEnum;
import com.eastfair.venueservice.vo.ServiceProviderSpaceVO;
import com.eastfair.venueservice.vo.SpaceAcceptanceVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 主运展厅
 * </p>
 *
 * @author linan
 */
@Slf4j
@Service
public class HomeSpaceServiceImpl implements HomeSpaceService {

    @Resource
    private ServiceProviderSpaceFeign serviceProviderSpaceFeign;
    @Resource
    private SpaceAcceptanceFeign spaceAcceptanceFeign;

    @Override
    public List<SpaceVO> getSpaceCanAccept(ExhibitionServiceProviderDTO dto) {
        // 查询主运展厅
        ServiceProviderSpaceQuery serviceProviderSpaceQuery = new ServiceProviderSpaceQuery();
        serviceProviderSpaceQuery.setExhibitionManageId(dto.getExhibitionManageId());
        serviceProviderSpaceQuery.setServiceProviderId(dto.getServiceProviderId());
        R<List<ServiceProviderSpaceVO>> serviceProviderSpace = serviceProviderSpaceFeign.queryServiceProviderSpace(serviceProviderSpaceQuery);
        // 查询是否已经在验收
        SpaceAcceptanceQuery spaceAcceptanceQuery = new SpaceAcceptanceQuery();
        spaceAcceptanceQuery.setCompanyId(dto.getServiceProviderId());
        spaceAcceptanceQuery.setExhibitionManageId(dto.getExhibitionManageId());
        PageParams<SpaceAcceptanceQuery> params = new PageParams<>();
        params.setModel(spaceAcceptanceQuery);
        params.setSize(999999);
        params.setCurrent(0);
        R<Page<SpaceAcceptanceVO>> iPageR = spaceAcceptanceFeign.querySpaceAcceptance(params);
        Map<String, List<SpaceAcceptanceVO>> spaceAcceptanceMap = ConvertUtil.convertList(iPageR.getData().getRecords(),SpaceAcceptanceVO.class).stream().collect(Collectors.groupingBy(SpaceAcceptanceVO::getSpaceCode));
        ArrayList<SpaceVO> spaceVOS = new ArrayList<>();
        for (ServiceProviderSpaceVO spaceVO : serviceProviderSpace.getData()) {
            List<SpaceAcceptanceVO> spaceAcceptanceVOS = spaceAcceptanceMap.get(spaceVO.getSpaceCode());
            if (CollectionUtils.isEmpty(spaceAcceptanceVOS)) {
                spaceVOS.add(BeanUtil.copyProperties(spaceVO, SpaceVO.class));
            } else {
                // 除了已取消，不存在其他类型记录，则添加
                if(CollectionUtils.isEmpty(spaceAcceptanceVOS.stream().filter(e->!e.getFinishStatus().eq(SpaceAcceptanceFinishStatusEnum.CANCEL)).collect(Collectors.toList()))){
                    spaceVOS.add(BeanUtil.copyProperties(spaceVO, SpaceVO.class));
                }
            }
        }
        return spaceVOS;
    }
}
